<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- 加载中 loading -->
    <van-loading class="article-loading" />
    <!-- /加载中 loading -->
    <!-- 文章详情 -->
    <div class="detail">
        <h3 class="title">{{article.title}}</h3>
        <!-- 作者 -->
        <div class="author">
          <van-image
            round width="1rem" height="1rem" fit="fill" :src="article.aut_photo" />
          <div class="text">
            <p class="name">{{article.aut_name}}</p>
            <p class="time">{{article.pubdate | formatTime}}</p>
          </div>
          <van-button
            round
            size="small"
            type="info"
            v-show="!isYourself"
            @click="hSwitchFollow">
            {{article.is_followed ? '取关': '+ 关注'}}</van-button>
        </div>
        <!-- 作者 end-->
        <div class="content" v-html="article.content"></div>
        <van-divider>END</van-divider>
        <!-- 点赞/喜欢 -->
        <div class="zan">
            <van-button round size="small" hairline type="primary" plain icon="good-job-o" @click="hzan">{{isZan?'取消点赞':'点赞'}}</van-button>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <van-button round size="small" hairline type="danger" plain icon="delete">不喜欢</van-button>
        </div>
        <!-- 文章评论 -->
        <article-comment :article_id="$route.params.id"></article-comment>        <!--  -->
    </div>

  </div>
</template>

<script>
import { getArticle, liking, unliking } from '@/api/article.js'
import { unFollowUser, followUser } from '@/api/user.js'
import ArticleComment from './comment'
export default {
  name: 'Article',
  data () {
    return {
      // 当前文章
      article: [],
      // 文章是否存在
      is404: false,
      // 控制加载中的 loading 状态
      loading: true,
      // 点赞状态
      isZan: false
    }
  },
  computed: {
    isYourself () {
      if (!this.article.aut_id) {
        return false
      }
      // eslint-disable-next-line eqeqeq
      return this.article.aut_id == this.$store.state.userInfo.id
    }
  },
  created () {
    this.loadArtice()
  },
  components: {
    ArticleComment
  },
  methods: {
    // 文章展示
    async loadArtice () {
      const id = this.$route.params.id
      const result = await getArticle(id)
      this.article = result.data.data
      console.log(result);
      this.loading = false // 结束loading
    },
    // 关注/取消关注用户
    async hSwitchFollow () {
      try {
        const isFollowed = this.article.is_followed
        const autId = this.article.aut_id
        if (isFollowed) {
          const result = await unFollowUser(autId)
          console.log(result);
        } else {
          const result = await followUser(autId)
          console.log(result);
        }
        // 更新视图
        // 在本地取反
        this.article.is_followed = !isFollowed
        this.$toast.success('操作成功')
      } catch (e) {
        console.log(e);
        this.$toast.fail('操作失败')
      }
    },
    // 点赞
    async hzan () {
      try {
        const articleId = this.$route.params.id
        if (this.isZan) {
          const result1 = await unliking(articleId)
          console.log(result1);
        } else {
          const result = await liking(articleId)
          console.log(result);
        }
        // console.log(this.isZan);
        this.isZan = !this.isZan
        this.$toast.success('操作成功')
      } catch (e) {
        console.log(e);
        this.$toast.fail('操作失败')
      }
    },
    // 特殊路由跳转
    // 文章详情页退出登录后，在登录--进入文章列表，不进入文章详情
    beforeRouteLeave (to, from, next) {
      console.log('你现在在文章详情页，你将要离开这个页面')
      console.log(to, from)
      if (to.path === '/login' && this.$store.state.tokenInfo.token) {
        next({ path: '/' })
      } else {
        next()
      }
    },
    destroyed () {
      console.log('文章详情页 destroyed')
    }
  }
}
</script>

<style scoped lang="less">
.article-container{
  // position: absolute;
  // left: 0;
  // top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.detail {
  padding: 0 10px;
}
.author {
padding: 10px 0;
display: flex;
.text {
flex: 1;
padding-left: 10px;
line-height: 1.3;
.name {
    font-size: 14px;
    margin: 0;
}
.time {
    margin: 0;
    font-size: 12px;
    color: #999;
}
}
}
.zan{
    text-align: center;
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
</style>
